package com.pansoft.nfc.acs;

import com.acs.bluetooth.BluetoothReader;

import static com.pansoft.nfc.acs.Utils.toHexString;
import static com.pansoft.nfc.mifare.MCManager.CHECK_KEY;

public class AcsBleManager {
    /**
     *  Default master key.
     */
    public static final String DEFAULT_1255_MASTER_KEY = "ACR1255U-J1 Auth";
    /**
     * 读操作，前缀。
     * 中间拼上要读取的块号：00h ~ 02h，UID及厂商信息，只读
     * 04h ~ 06h，...， 38h ~ 0Ah， 3Ch ~ 3Eh
     */
    public static final String APDU_COMMAND_READ_PREFIX = "FF B0 00";
    /**
     * 读操作后缀，代表一次读取的字数，1-16，默认10h
     */
    public static final String APDU_COMMAND_READ_SUFFIX = "10";

    /**
     * 写操作， FF D6 00 XX YY
     */
    public static final String APDU_COMMAND_WRITE_PREFIX = "FF D6 00";

    /**
     * 加载秘钥， FF 82 00 00 06 FF FF FF FF FF FF, 后十二位是秘钥
     * 第4个字节 代表位置 00h-01h
     **/
    public static final String APDU_COMMAND_LOAD_AUTH = "FF 82 00 00 06 " + CHECK_KEY;

    /**
     * 验证， FF 86 00 00 05 01 00 XX 60 00, XX 是块号
     * 后面拼上块号
     **/
    public static final String APDU_COMMAND_AUTH_PREFIX = "FF 86 00 00 05 01 00";
    /**
     * 验证 后缀，60h A秘钥， 00h秘钥位置
     */
    public static final String APDU_COMMAND_AUTH_SUFFIX = "60 00";

    /* Get the Response string. */
    public static String getResponseString(byte[] response, int errorCode) {
        if (errorCode == BluetoothReader.ERROR_SUCCESS) {
            if (response != null && response.length > 0) {
                return toHexString(response);
            }
            return "";
        }
        return getErrorString(errorCode);
    }

    /* Get the Error string. */
    public static String getErrorString(int errorCode) {
        if (errorCode == BluetoothReader.ERROR_SUCCESS) {
            return "";
        } else if (errorCode == BluetoothReader.ERROR_INVALID_CHECKSUM) {
            return "The checksum is invalid.";
        } else if (errorCode == BluetoothReader.ERROR_INVALID_DATA_LENGTH) {
            return "The data length is invalid.";
        } else if (errorCode == BluetoothReader.ERROR_INVALID_COMMAND) {
            return "The command is invalid.";
        } else if (errorCode == BluetoothReader.ERROR_UNKNOWN_COMMAND_ID) {
            return "The command ID is unknown.";
        } else if (errorCode == BluetoothReader.ERROR_CARD_OPERATION) {
            return "The card operation failed.";
        } else if (errorCode == BluetoothReader.ERROR_AUTHENTICATION_REQUIRED) {
            return "Authentication is required.";
        } else if (errorCode == BluetoothReader.ERROR_LOW_BATTERY) {
            return "The battery is low.";
        } else if (errorCode == BluetoothReader.ERROR_CHARACTERISTIC_NOT_FOUND) {
            return "Error characteristic is not found.";
        } else if (errorCode == BluetoothReader.ERROR_WRITE_DATA) {
            return "Write command to reader is failed.";
        } else if (errorCode == BluetoothReader.ERROR_TIMEOUT) {
            return "Timeout.";
        } else if (errorCode == BluetoothReader.ERROR_AUTHENTICATION_FAILED) {
            return "Authentication is failed.";
        } else if (errorCode == BluetoothReader.ERROR_UNDEFINED) {
            return "Undefined error.";
        } else if (errorCode == BluetoothReader.ERROR_INVALID_DATA) {
            return "Received data error.";
        } else if (errorCode == BluetoothReader.ERROR_COMMAND_FAILED) {
            return "The command failed.";
        }
        return "Unknown error.";
    }
}
